package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.MesActionTherbligsType;
import mesweb.mesweb.service.impl.MesActionTherbligsTypeServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/mes/mes-action-therbligs-type")
@Api(tags = "MesActionTherbligsTypeController", description = "动素类型管理")
public class MesActionTherbligsTypeController {
    @Autowired
    private MesActionTherbligsTypeServiceImpl MesActionTherbligsTypeService;

    @GetMapping("/GetAllActionTherbligsTypes")
    @ApiOperation("获取动素类型列表")
    public CommonResult<List<MesActionTherbligsType>> GetAllActionTherbligsTypes(@ApiParam("动素类型名称") @RequestParam(value = "name", required = false) String name) {
        List<MesActionTherbligsType> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = MesActionTherbligsTypeService.list();
        } else {
            QueryWrapper<MesActionTherbligsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("Therbligs_Type_Name", name);
            data = MesActionTherbligsTypeService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

    @GetMapping("/GetActionTherbligsType")
    @ApiOperation("根据编号获取动素类型")
    public CommonResult<MesActionTherbligsType> GetActionTherbligsTypeByNo(@ApiParam("动素类型编号") @RequestParam("no") String no) {
        if (no.isEmpty())
            return null;
        QueryWrapper<MesActionTherbligsType> queryWrapper = new QueryWrapper<MesActionTherbligsType>();
        queryWrapper.eq("Therbligs_Type_No", no);
        return CommonResult.success(MesActionTherbligsTypeService.getOne(queryWrapper));
    }

    @PostMapping("/SaveActionTherbligsType")
    @ApiOperation("保存动素类型")
    public CommonResult<Boolean> SaveActionTherbligsType(@ApiParam("动素类型") @RequestBody MesActionTherbligsType mesActionTherbligsType) {
        if (mesActionTherbligsType == null)
            return null;

        try {
            boolean result = false;
            if (mesActionTherbligsType.getId() == 0L) {
                QueryWrapper<MesActionTherbligsType> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("Therbligs_type_No", mesActionTherbligsType.getTherbligsTypeNo());
                queryWrapper.or().eq("Therbligs_type_Name", mesActionTherbligsType.getTherbligsTypeName());
                MesActionTherbligsType mesTechnologyFlow1 = MesActionTherbligsTypeService.getOne(queryWrapper);
                if (mesTechnologyFlow1 != null) {
                    String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getTherbligsTypeNo(), mesTechnologyFlow1.getTherbligsTypeName());
                    throw new ApiException(msg);
                }
                result = MesActionTherbligsTypeService.save(mesActionTherbligsType);
            } else {
                result = MesActionTherbligsTypeService.update(mesActionTherbligsType, null);
            }
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }

    @DeleteMapping("/DeleteActionTherbligsType/{Id}")
    @ApiOperation("删除动素类型")
    public CommonResult<Boolean> DeleteActionTherbligsType(@ApiParam("动素类型ID") @PathVariable("Id") int Id) {
        try {
            boolean result = MesActionTherbligsTypeService.removeById(Id);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }

    @DeleteMapping("/DeleteActionTherbligsTypes")
    @ApiOperation("批量删除动素类型")
    public CommonResult<Boolean> DeleteActionTherbligsType(@ApiParam("动素类型IDs") @RequestBody List<Long> ids) {
        try {
            boolean result = MesActionTherbligsTypeService.removeByIds(ids);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
}
